package com.fighYo.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fighYo.edu.entity.EduChapter;
import com.fighYo.edu.entity.EduVideo;
import com.fighYo.edu.entity.vo.VideoVo;
import com.fighYo.edu.mapper.EduVideoMapper;
import com.fighYo.edu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fighYo.servicebase.config.handler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author fighYo
 * @since 2021-08-24
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Override
    public void saveVideo(VideoVo videoVo) {
        EduVideo eduVideo = new EduVideo();
        BeanUtils.copyProperties(videoVo, eduVideo);
        int insert = baseMapper.insert(eduVideo);
        if(insert == 0) {
            throw new GuliException(20001, "课时信息保存失败");
        }
    }

    @Override
    public void updateVideo(VideoVo videoVo) {
        EduVideo eduVideo = new EduVideo();
        BeanUtils.copyProperties(videoVo, eduVideo);
        int update = baseMapper.updateById(eduVideo);
        if(update == 0) {
            throw new GuliException(20001, "课时修改保存失败");
        }
    }

    @Override
    public VideoVo getVideoById(String id) {
        EduVideo eduVideo = baseMapper.selectById(id);
        if(eduVideo == null) {
            throw new GuliException(20001, "数据不存在");
        }
        VideoVo videoVo = new VideoVo();
        BeanUtils.copyProperties(eduVideo, videoVo);
        return videoVo;
    }

    @Override
    public boolean removeVideoByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        int delete = baseMapper.delete(wrapper);
        return delete > 0;
    }
}
